package com.bfxy.kafka.api.consumer.rebalance;

import com.bfxy.kafka.api.constant.Const;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

public class RebalanceConsumer2 {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.218.21:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        // GROUP_ID_CONFIG：消费者组配置
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "rebalance-group");
        properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 10000);
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 5000);
        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties)) {
            // 配置消费者在均衡 ConsumerRebalanceListener
            consumer.subscribe(Collections.singletonList(Const.TOPIC_REBALANCE), new ConsumerRebalanceListener() {
                /**
                 * partitions回收
                 * @param partitions
                 */
                @Override
                public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
                    System.out.println("Revoked Partitions：" + partitions);
                }

                /**
                 * partitions重新分配
                 * @param partitions
                 */
                @Override
                public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                    System.out.println("Assigned Partitions：" + partitions);
                }
            });
            System.out.println("rebalance consumer2 started...");
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
                for (TopicPartition partition : records.partitions()) {
                    List<ConsumerRecord<String, String>> partitionRecordList = records.records(partition);
                    String topic = partition.topic();
                    int size = partitionRecordList.size();
                    System.out.println(String.format("--- 获取topic：%s，分区位置：%s，消息总数：%s ---", topic, partition.partition(), size));
                    for (ConsumerRecord<String, String> consumerRecord : partitionRecordList) {
                        String value = consumerRecord.value();
                        long offset = consumerRecord.offset();
                        long commitOffset = offset + 1;
                        System.out.println(String.format("-- 获取实际消息value：%s，消息offset：%s，提交offset：%s ---", value, offset, commitOffset));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
